package besta.moon.sql.expression.function.string;

import besta.moon.sql.expression.function.FunctionReturnInt;
import besta.moon.sql.parser.SQLTokenizer;
import besta.moon.util.MoonUtils;

public final class FunctionAscii extends FunctionReturnInt {

    @Override
    public final int getFunction() {
        return SQLTokenizer.ASCII;
    }

    @Override
    public final boolean isNull() throws Exception {
        return param1.isNull() || param1.getString().length() == 0;
    }

    @Override
    public final int getInt() throws Exception {
        String str = param1.getString();
        if (str == null || str.length() == 0) {
            return 0;
        }
        return str.charAt(0);
    }

    @Override
    public final Object getObject() throws Exception {
        String str = param1.getString();
        if (str == null || str.length() == 0) {
            return null;
        }
        return MoonUtils.getInteger(str.charAt(0));
    }
}
